<?php
/**
 * Created by PhpStorm.
 * User: 陶然
 * Date: 2019/4/11
 * Time: 21:12
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是陶然,健康撸代码,拒绝通宵。
 * CLASS: < 公用方法 >
 */
namespace App\HttpController\Taoran\Functions;

use App\HttpController\V1\Base;

class Publics extends Base
{
    /**
     * @param $ids  执行用户
     * @param $money  触发奖学金数量
     * @param $type  类别{1:升级vip,2:购买商品,3:报名活动}
     * @param $db    db
     * @return bool
     * 感召激发奖
     */
    function call_inspiration($ids,$money,$type,$db,$goods_id=null)
    {
        switch ($type){
            case 1:
                $call='vip_ganzhao';
                $inspire='vip_jifa';
                $userPid=$db->where('id',$ids)->getOne('users');
                $pidData=$db->where('id',$userPid['pid'])->getOne('users'); //上级
                if (!$pidData){
                    $pidData=$userPid;
                }

                $log=[];
                if (!in_array($pidData['level'],[1,4])){
                    $call=($db->where('name',$call)->getValue('config','value'))/100;
                    $log[]=Databases::user_accounts_log($pidData['id'],$ids,1,'study',1,$money*$call,$pidData['balance'],'奖金比例:'.$call);
                    $db->where('id',$pidData['id'])->setInc('users','balance',$money*$call);
                }
                $UnPidData=$db->where('id',$pidData['pid'])->getOne('users');

                if (!$UnPidData){
                    $UnPidData=$userPid;
                }
                if (!in_array($UnPidData['level'],[1,4])){
                    $inspire=($db->where('name',$inspire)->getValue('config','value'))/100;
                    $log[]=Databases::user_accounts_log($UnPidData['id'],$ids,2,'study',1,$money*$inspire,$UnPidData['balance'],'奖金比例:'.$inspire);
                    $db->where('id',$UnPidData['id'])->setInc('users','balance',$money*$inspire);
                }
                if (!empty($log)){
                    $sql=$db->insertMulti('users_account_log',$log);
                    if (!$sql){
                        return false;
                    }
                }
                return true;
                break;
            case 2:
                $goods_info=$db->where('id',$goods_id)->getOne('goods','ganzhao,jifa');
                $call=$goods_info['ganzhao']/100;
                $inspire=$goods_info['jifa']/100;
                $userPid=$db->where('id',$ids)->getOne('users');
                $pidData=$db->where('id',$userPid['pid'])->getOne('users'); //上级
                if (!$pidData){
                    $pidData=$userPid;
                }

                $log=[];
                $log[]=Databases::user_accounts_log($pidData['id'],$ids,1,'study',1,$money*$call,$pidData['balance'],'奖金比例:'.$call);
                $db->where('id',$pidData['id'])->setInc('users','balance',$money*$call);
                $UnPidData=$db->where('id',$pidData['pid'])->getOne('users');

                if (!$UnPidData){
                    $UnPidData=$userPid;
                }
                $log[]=Databases::user_accounts_log($UnPidData['id'],$ids,2,'study',1,$money*$inspire,$UnPidData['balance'],'奖金比例:'.$inspire);
                $db->where('id',$UnPidData['id'])->setInc('users','balance',$money*$inspire);
                if (!empty($log)){
                    $sql=$db->insertMulti('users_account_log',$log);
                    if (!$sql){
                        return false;
                    }
                }
                return true;
                break;
            case 3:
                $call='call_inspiration';
                $inspire='inspire';
                $userPid=$db->where('id',$ids)->getOne('users');
                $pidData=$db->where('id',$userPid['pid'])->getOne('users'); //上级
                if (!$pidData){
                    $pidData=$userPid;
                }
                $log=[];
                $call=($db->where('name',$call)->getValue('config','value'))/100;
                $log[]=Databases::user_accounts_log($pidData['id'],$ids,1,'study',1,$money*$call,$pidData['balance'],'奖金比例:'.$call);
                $db->where('id',$pidData['id'])->setInc('users','balance',$money*$call);
                $UnPidData=$db->where('id',$pidData['pid'])->getOne('users');

                if (!$UnPidData){
                    $UnPidData=$userPid;
                }

                $inspire=($db->where('name',$inspire)->getValue('config','value'))/100;
                $log[]=Databases::user_accounts_log($UnPidData['id'],$ids,2,'study',1,$money*$inspire,$UnPidData['balance'],'奖金比例:'.$inspire);
                $db->where('id',$UnPidData['id'])->setInc('users','balance',$money*$inspire);

                if (!empty($log)){
                    $sql=$db->insertMulti('users_account_log',$log);
                    if (!$sql){
                        return false;
                    }
                }
                return true;
                break;
            default:
                return true;
                break;
        }

    }

    /**
     * 集体贡献奖
     * 类别{1:升级vip,2:购买商品}
     */
    function group($ids,$money,$type,$db){
        $user=$db->where('id',$ids)->getOne('users');
        if(!$user){
            return true;
        }
        $pidData=$db->where('id',$user['pid'])->getOne('users'); //上级
        if ($pidData['level']!==3){
            $this->group($pidData['id'],$money,$type,$db);
            return true;
        }
        $call=($db->where('name','group')->getValue('config','value'))/100;
        $inspire=($db->where('name','goods_value')->getValue('config','value'))/100;
        switch ($type){
            case 1:
                if(in_array($user['level'],[1,2,4])&&$pidData['level']==3){
                    $log= Databases::user_accounts_log($pidData['id'],$ids,4,'study',1,$money*$call,$pidData['balance'],'奖金比例:'.$call);
                    $db->where('id',$pidData['id'])->setInc('users','balance',$money*$call);
                    if (!empty($log)){
                        $sql=$db->insert('users_account_log',$log);
                        if (!$sql){
                            return false;
                        }
                    }
                }
                return true;
                break;
            case 2:
                if(in_array($user['level'],[1,2,4])&&$pidData['level']==3){
                    $log= Databases::user_accounts_log($pidData['id'],$ids,4,'study',1,$money*$inspire*$call,$pidData['balance'],'奖金比例:'.$call);
                    $db->where('id',$pidData['id'])->setInc('users','balance',$money*$inspire*$call);
                    if (!empty($log)){
                        $sql=$db->insert('users_account_log',$log);
                        if (!$sql){
                            return false;
                        }
                    }
                }
                return true;
                break;
        }
    }

    /**
     * 时间转换 分/秒
     * @param null $time
     * @return false|string
     */
    function get_last_time($time = NULL) {
        $time=strtotime($time);
        $t = time() - $time; //时间差 （秒）
        switch($t){
            case $t == 0||$t<0:
                $text = '刚刚';
                break;
            case $t < 60:
                $text = $t . '秒前'; // 一分钟内
                break;
            case $t < 60 * 60:
                $text = floor($t / 60) . '分钟前'; //一小时内
                break;
            default:
                $text = date('Y/m/d/ H:i', $time);
                break;
        }
        return $text;
    }


}